<script setup lang="ts">
import CustomNavbar from './componets/CustomNavbar.vue'
import CategoryPanel from './componets/CategoryPanel.vue'
import HotPanel from './componets/HotPanel.vue'
import PageSkeleton from './componets/PageSkeleton.vue'
import { getHomeBannerAPI, getHomeCategoryAPI, getHomeHotAPI } from '@/services/home'
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue'
import type { BannerItem, CategoryItem, HotItem } from '@/types/home'
import type { XtxGuessInstance } from '@/types/component'
//轮播图信息
const HomeBannerList = ref<BannerItem[]>([])
//商品分类信息
const categoryList = ref<CategoryItem[]>([])
//热门推荐
const HotPanelList = ref<HotItem[]>([])
//猜你喜欢
// 获取猜你喜欢组件实例
const guessRef = ref<XtxGuessInstance>()

// 滚动触底事件
const onScrolltolower = () => {
  guessRef.value?.getMore()
}
// 当前下拉刷新状态
const isTriggered = ref(false)
// 自定义下拉刷新被触发
const onRefresherrefresh = async () => {
  // 开始动画
  isTriggered.value = true
  guessRef.value?.resetData()
  await Promise.all([
    getHomeBannerData(),
    getHomeCategoryData(),
    getHomeHotData(),
    guessRef.value?.getMore(),
  ])
  // 关闭动画
  isTriggered.value = false
}

//页面初次加载时
onLoad(async () => {
  isTriggered.value = true
  await Promise.all([getHomeBannerData(), getHomeCategoryData(), getHomeHotData()])
  isTriggered.value = false
})

//=========================接口层
//获取轮播图数据
const getHomeBannerData = async () => {
  const res: any = await getHomeBannerAPI()
  if (res.code == 1) {
    HomeBannerList.value = res.result
  } else {
    uni.showToast({
      icon: 'none',
      title: res.msg,
    })
  }
}
//获取商品分类数据
const getHomeCategoryData = async () => {
  const res: any = await getHomeCategoryAPI()
  if (res.code == 1) {
    categoryList.value = res.result
  } else {
    uni.showToast({
      icon: 'none',
      title: res.msg,
    })
  }
}
//获取商品分类数据
const getHomeHotData = async () => {
  const res: any = await getHomeHotAPI()
  if (res.code == 1) {
    HotPanelList.value = res.result
  } else {
    uni.showToast({
      icon: 'none',
      title: res.msg,
    })
  }
}
</script>

<template>
  <view class="viewport">
    <!-- 头部导航栏 -->
    <CustomNavbar />
    <!-- 滚动容器 -->
    <scroll-view
      enable-back-to-top
      refresher-enabled
      @refresherrefresh="onRefresherrefresh"
      :refresher-triggered="isTriggered"
      @scrolltolower="onScrolltolower"
      class="scroll-view"
      scroll-y
    >
      <PageSkeleton v-if="isTriggered" />
      <template v-else>
        <!-- 轮播图 -->
        <XtxSwiper :list="HomeBannerList" />
        <!-- 商品分类 -->
        <CategoryPanel :list="categoryList" />
        <!-- 热门推荐布局 -->
        <HotPanel :list="HotPanelList" />
        <!-- 猜你喜欢 -->
        <XtxGuess ref="guessRef" :list="HomeBannerList" />
      </template>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  background-color: #f7f7f7;
  height: 100%;
  overflow: hidden;
}

.viewport {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.scroll-view {
  flex: 1;
  overflow: hidden;
}
</style>
